package com.skyrin.draglayoutdemo;

import android.graphics.Outline;
import android.support.constraint.Guideline;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayout;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView tv_area;

    TextView tv_bottom1;
    TextView tv_top2;
    TextView tv_bottom2;
    TextView tv_top1;

    Guideline guideline;

    DragConstraintLayout drag_layout;

    /* The circular outline provider */
    private ViewOutlineProvider mOutlineProviderCircle;

    /* The current elevation of the floating view. */
    private float mElevation = 0;

    /* The step in elevation when changing the Z value */
    private int mElevationStep;

    private GridLayout gd_users;

    int tv_area_height;

    boolean dragInArea = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_bottom1 = findViewById(R.id.tv_bottom1);
        tv_top2 = findViewById(R.id.tv_top2);
        tv_bottom2 = findViewById(R.id.tv_bottom2);
        tv_top1 = findViewById(R.id.tv_top1);
        tv_area = findViewById(R.id.tv_area);
        drag_layout = findViewById(R.id.drag_layout);
        gd_users = findViewById(R.id.gd_users);
        guideline = findViewById(R.id.guideline);

        tv_area_height = dp2px(56);

        initDragView();
    }

    private void initDragView() {
        mOutlineProviderCircle = new CircleOutlineProvider();
        mElevationStep = getResources().getDimensionPixelSize(R.dimen.elevation_step);

        drag_layout.setDragConstraintController(new DragConstraintLayout.DragConstraintLayoutController() {
            @Override
            public void onDragDrop(View view, boolean captured) {

                view.animate()
                        .translationZ(captured ? 50 : 0)
                        .setDuration(100);

                toggleArea(captured);

                if (!captured && dragInArea) {
//                    ((ViewGroup)view.getParent()).removeView(view);
                    view.setVisibility(View.GONE);
                    adJustGuideLine();
//                    gd_users.addView(view);
                } else {
                    // 归位
                }
            }

            @Override
            public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
                dragInArea = changedView.getY() < tv_area_height;
            }
        });

        drag_layout.addDragView(tv_top1);
        drag_layout.addDragView(tv_top2);
        drag_layout.addDragView(tv_bottom1);
        drag_layout.addDragView(tv_bottom2);
    }

    /**
     * adjust GuideLine 0:top 0.5:mid 1:bottom
     */
    private void adJustGuideLine() {
        guideline.setGuidelinePercent(0.5f);
        if (tv_top1.getVisibility() == View.GONE && tv_top2.getVisibility() == View.GONE) {
            if (tv_bottom1.getVisibility() == View.GONE || tv_bottom2.getVisibility() == View.GONE) {
                guideline.setGuidelinePercent(0.0f);
            } else {

            }
        }
        if (tv_bottom1.getVisibility() == View.GONE && tv_bottom2.getVisibility() == View.GONE) {
            guideline.setGuidelinePercent(1.0f);
        }
    }

    void toggleArea(boolean captured) {
        if (captured) {
            tv_area.setBackgroundColor(getResources().getColor(R.color.haf_trans));
        } else {
            tv_area.setBackgroundColor(getResources().getColor(R.color.full_trans));
        }
    }

    /**
     * ViewOutlineProvider which sets the outline to be an oval which fits the view bounds.
     */
    private class CircleOutlineProvider extends ViewOutlineProvider {
        @Override
        public void getOutline(View view, Outline outline) {
            outline.setOval(0, 0, view.getWidth(), view.getHeight());
        }
    }

    /**
     * Value of dp to value of px.
     *
     * @param dpValue The value of dp.
     * @return value of px
     */
    public int dp2px(final float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * Value of px to value of dp.
     *
     * @param pxValue The value of px.
     * @return value of dp
     */
    public int px2dp(final float pxValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
